Get Started with using Pkg in Julia

Learn how to get started with using the Package Manager in Julia
Published

April 30, 2023

Introduction

Julia, a high-level and high-performance programming language, is well-suited for various scientific and technical computing applications. One of its key features is the Package Manager (Pkg), which allows you to easily manage and install packages. In this blog post, we’ll explore how to get started with using Pkg in Julia.

Getting Started

To start using Pkg in Julia, you’ll first need to import it using the following command:

import Pkg

Installing Packages

To install a new package, you can use the Pkg.add() function. For example, let’s say we want to install the popular DataFrames package:

Pkg.add("DataFrames")
    Updating registry at `~/.julia/registries/General.toml`
   Resolving package versions...
    Updating `~/gitrepos/juliacheat.codes/how-to/get-started-with-using-pkg/Project.toml`
  [a93c6f00] + DataFrames v1.5.0
    Updating `~/gitrepos/juliacheat.codes/how-to/get-started-with-using-pkg/Manifest.toml`
  [34da2185] + Compat v4.6.1
  [a8cc5b0e] + Crayons v4.1.1
  [9a962f9c] + DataAPI v1.14.0
  [a93c6f00] + DataFrames v1.5.0
  [864edb3b] + DataStructures v0.18.13
  [e2d170a0] + DataValueInterfaces v1.0.0
  [59287772] + Formatting v0.4.2
  [842dd82b] + InlineStrings v1.4.0
  [41ab1584] + InvertedIndices v1.3.0
  [82899510] + IteratorInterfaceExtensions v1.0.0
  [b964fa9f] + LaTeXStrings v1.3.0
  [e1d29d7a] + Missings v1.1.0
  [bac558e1] + OrderedCollections v1.6.0
  [69de0a69] + Parsers v2.5.8
  [2dfb63ee] + PooledArrays v1.4.2
  [21216c6a] + Preferences v1.3.0
  [08abe8d2] + PrettyTables v2.2.3
  [189a3867] + Reexport v1.2.2
  [91c51154] + SentinelArrays v1.3.18
  [66db9d55] + SnoopPrecompile v1.0.3
  [a2af1166] + SortingAlgorithms v1.1.0
  [892a3eda] + StringManipulation v0.3.0
  [3783bdb8] + TableTraits v1.0.1
  [bd369af6] + Tables v1.10.1
  [56f22d72] + Artifacts
  [2a0f44e3] + Base64
  [ade2ca70] + Dates
  [9fa8497b] + Future
  [b77e0a4c] + InteractiveUtils
  [8f399da3] + Libdl
  [37e2e46d] + LinearAlgebra
  [56ddb016] + Logging
  [d6f4376e] + Markdown
  [de0858da] + Printf
  [3fa0cd96] + REPL
  [9a3f8284] + Random
  [ea8e919c] + SHA v0.7.0
  [9e88b42a] + Serialization
  [6462fe0b] + Sockets
  [2f01184e] + SparseArrays
  [10745b16] + Statistics
  [fa267f1f] + TOML v1.0.0
  [8dfed614] + Test
  [cf7118a7] + UUIDs
  [4ec0a83e] + Unicode
  [e66e0078] + CompilerSupportLibraries_jll v1.0.1+0
  [4536629a] + OpenBLAS_jll v0.3.20+0
  [8e850b90] + libblastrampoline_jll v5.1.1+0

This command will download and install the DataFrames package and its dependencies.

Updating Packages

To update all installed packages to their latest versions, use the Pkg.update() function:

Pkg.update()
    Updating registry at `~/.julia/registries/General.toml`
  No Changes to `~/gitrepos/juliacheat.codes/how-to/get-started-with-using-pkg/Project.toml`
  No Changes to `~/gitrepos/juliacheat.codes/how-to/get-started-with-using-pkg/Manifest.toml`

Removing Packages

If you no longer need a package, you can remove it using the Pkg.rm() function:

Pkg.rm("DataFrames")
    Updating `~/gitrepos/juliacheat.codes/how-to/get-started-with-using-pkg/Project.toml`
  [a93c6f00] - DataFrames v1.5.0
    Updating `~/gitrepos/juliacheat.codes/how-to/get-started-with-using-pkg/Manifest.toml`
  [34da2185] - Compat v4.6.1
  [a8cc5b0e] - Crayons v4.1.1
  [9a962f9c] - DataAPI v1.14.0
  [a93c6f00] - DataFrames v1.5.0
  [864edb3b] - DataStructures v0.18.13
  [e2d170a0] - DataValueInterfaces v1.0.0
  [59287772] - Formatting v0.4.2
  [842dd82b] - InlineStrings v1.4.0
  [41ab1584] - InvertedIndices v1.3.0
  [82899510] - IteratorInterfaceExtensions v1.0.0
  [b964fa9f] - LaTeXStrings v1.3.0
  [e1d29d7a] - Missings v1.1.0
  [bac558e1] - OrderedCollections v1.6.0
  [69de0a69] - Parsers v2.5.8
  [2dfb63ee] - PooledArrays v1.4.2
  [21216c6a] - Preferences v1.3.0
  [08abe8d2] - PrettyTables v2.2.3
  [189a3867] - Reexport v1.2.2
  [91c51154] - SentinelArrays v1.3.18
  [66db9d55] - SnoopPrecompile v1.0.3
  [a2af1166] - SortingAlgorithms v1.1.0
  [892a3eda] - StringManipulation v0.3.0
  [3783bdb8] - TableTraits v1.0.1
  [bd369af6] - Tables v1.10.1
  [56f22d72] - Artifacts
  [2a0f44e3] - Base64
  [ade2ca70] - Dates
  [9fa8497b] - Future
  [b77e0a4c] - InteractiveUtils
  [8f399da3] - Libdl
  [37e2e46d] - LinearAlgebra
  [56ddb016] - Logging
  [d6f4376e] - Markdown
  [de0858da] - Printf
  [3fa0cd96] - REPL
  [9a3f8284] - Random
  [ea8e919c] - SHA v0.7.0
  [9e88b42a] - Serialization
  [6462fe0b] - Sockets
  [2f01184e] - SparseArrays
  [10745b16] - Statistics
  [fa267f1f] - TOML v1.0.0
  [8dfed614] - Test
  [cf7118a7] - UUIDs
  [4ec0a83e] - Unicode
  [e66e0078] - CompilerSupportLibraries_jll v1.0.1+0
  [4536629a] - OpenBLAS_jll v0.3.20+0
  [8e850b90] - libblastrampoline_jll v5.1.1+0

Listing Installed Packages

To see a list of all installed packages and their versions, use the Pkg.status() function:

Pkg.status()
Status `~/gitrepos/juliacheat.codes/how-to/get-started-with-using-pkg/Project.toml` (empty project)

Further Resources

To learn more about the Package Manager (Pkg) in Julia, you can refer to the official Pkg documentation.

We hope this blog post has provided you with a solid foundation for using the Package Manager in Julia. Happy coding!

Reuse

Citation

For attribution, please cite this work as:
“Get Started with using Pkg in Julia,” Apr. 30, 2023. https://juliacheat.codes/how-to/get-started-with-using-pkg.