Using GHCup!
When it comes to starting out with Haskell, I usually recommend installing Stack. Stack is an effective one-stop shop. It automatically installs Cabal for you, and it's also able to install the right version of GHC for your project. It installs GHC to a sandboxed location, so you can easily use different versions of GHC for different projects.
But there's another good program that can help with these needs! This program is called GHCup ("GHC up"). It fills a slightly different role from Stack, and it actually allows more flexibility in certain areas. Let's see how it works!
How do I install GHCup?
Just like Stack, you can install GHCup with a single terminal command. Per the documentation, you can use this command on Linux, Mac, and Windows Subsystem for Linux:
curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh
See the link above for special instructions on a pure Windows setup.
What does GHCup do?
GHCup can handle the installation of all the important programs that make Haskell work. This includes, of course, the compiler GHC, Cabal, and Stack itself. What makes it special is that it can rapidly toggle between the different versions of all these programs, which can give you more flexibility.
Once you install GHCup, this should install the recommended version of each of these. You can see what is installed with the command ghcup list
.
The "currently installed" version of each has a double checkmark as you can see in the picture. When you use each of these commands with the --version
argument, you should see the version indicated by GHCup:
>> stack --version
Version 2.7.5
>> cabal --version
cabal-install version 3.6.2.0
>> ghc --version
The Glorious Glasgow Haskell Compilation System, version 9.02
How do I switch versions with GHCup?
Any entry with a single green checkmark is "installed" on your system but not "set". You can set it as the "global" version with the ghcup set
command.
>> ghcup set ghc 8.10.7
[ Info ] GHC 8.10.7 successfully set as default version
>> ghc --version
The Glorious Glasgow Haskell Compilation System, version 8.10.7
Versions with a red x aren't installed but are available to download. If a version isn't installed on your system, you can use ghcup install
to get it:
>> ghcup install stack 2.7.1
Then you need to set
the version to use it:
>> ghcup set stack 2.7.1
>> stack --version
Version 2.7.1
Note that the specific example with Stack might not work if you originally installed Stack through its own installer before using GHCup.
GHCup User Interface
On most platforms, you can also use the command: ghcup tui
. This brings up a textual user interface that allows you to make these changes quickly! It will bring up a screen like this on your terminal, allowing you to use the arrow keys to set the versions as you desire.
All the commands are on screen, so it's very easy to use!
Notes on Stack and GHC
An important note on setting the "global" version of GHC is that this does not affect stack sandboxing. Even if you run ghcup set ghc 8.10.7
, this won't cause any problems for a stack project using GHC 9.02. It will build as normal using 9.02.
So why does it even matter what the global version of GHC is? Let's find out!
GHCup and IDEs
Why do I mention GHCup when my last article was talking about IDEs? Well the one other utility you can install and customize with GHCup is the Haskell Language Server, which shows up in the GHCup output as the program hls
. This is a special program that enables partial compilation, lint suggestions and library autocompletion within your IDE (among other useful features). As we'll explore in the next couple articles, Haskell Language Server can be a little tricky to use!
Even though Stack uses sandboxed GHC versions, HLS depends on the "global" version of GHC. And changing the "global" version to a particular version you've installed with stack is a little tricky if you aren't super familiar with Haskell's internals and also comfortable with the command line. So GHCup handles this smoothly.
Imagine we have two projects with different Stack resolvers (and in this case different GHC versions).
# stack.yaml #1
# (GHC 9.0.2)
resolver:
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/19.16
# stack.yaml #2
# (GHC 8.10.7)
resolver:
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/18.26
If we want to get code suggestions in our first project, we just need to run this command before open it in the editor:
ghcup set ghc 9.0.2
And if we then want to switch to our second project, we just need one command to get our hints again!
ghcup set ghc 8.10.7
And of course, in addition to switching the GHC version, GHCup installs HLS for you and allows you to switch its version to keep up with updates.
Conclusion
With a basic understanding of HLS and switching GHC versions, we're now in a good position to start designing a really strong Haskell IDE! In the next couple of articles, we'll see a couple examples of this!
Keep up to date with all the latest news on Monday Morning Haskell by subscribing to our mailing list! This will also give you access to our subscriber resources!